﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BluSteamServer.Model;
using System.Windows.Input;
using System.ComponentModel;

namespace BluSteamServer.ViewModel
{
    public class ServerViewModel : INotifyPropertyChanged
    {
        #region Fields

        public event PropertyChangedEventHandler PropertyChanged;

        // Model objects
        private ServerListener _serverListener;
        private EventListener _eventListener;
        private List<string> _registerdUsers;
        private List<string> _activeUsers;

        private string _serverStatus;

        // Commands
        private RelayCommand _startServerCommand;
        private RelayCommand _stopServerCommand;
        private RelayCommand _kickUserCommand;
        private RelayCommand _banUserCommand;

        #endregion

        #region Constructors

        public ServerViewModel()
        {
            // subscribe for notifications of when a new user has connected & disconnected to the server
            ConnectionManager.Instance.UserConnect += this.OnUserConnect;
            ConnectionManager.Instance.UserDisconnect += this.OnUserDisconnect;

            _eventListener = new EventListener();
            ConnectionManager.Instance.UserConnect += _eventListener.OnUserConnect;
            ConnectionManager.Instance.UserDisconnect += _eventListener.OnUserDisconnect;

            _activeUsers = new List<string>()
            {
                "Drewan",
                "Dominic",
                "Luke"
            };

            _serverStatus = "Server is not running...";
        }

        #endregion

        #region Properties

        public List<string> RegisteredUsers
        {
            get { return _registerdUsers; }
            private set
            {
                if (_registerdUsers != value)
                {
                    _registerdUsers = value;
                    OnPropertyChanged("RegisteredUsers");
                }
            }
        }

        public List<string> ActiveUsers
        {
            get { return _activeUsers; }
            private set
            {
                if (_activeUsers != value)
                {
                    _activeUsers = value;
                    OnPropertyChanged("ActiveUsers");
                }
            }
        }

        public string ServerStatus
        {
            get { return _serverStatus; }
            private set
            {
                if (_serverStatus != value)
                {
                    _serverStatus = value;
                    OnPropertyChanged("ServerStatus");
                }
            }
        }

        #endregion

        #region Commands

        public ICommand StartServerCommand
        {
            get
            {
                if (_startServerCommand == null)
                {
                    _startServerCommand = new RelayCommand(ParamArrayAttribute => this.StartServer());
                }
                return _startServerCommand;
            }
        }

        public ICommand StopServerCommand
        {
            get
            {
                if (_stopServerCommand == null)
                {
                    _stopServerCommand = new RelayCommand(ParamArrayAttribute => this.StopServer());
                }
                return _stopServerCommand;
            }
        }

        public ICommand KickUserCommand
        {
            get
            {
                if (_kickUserCommand == null)
                {
                    _kickUserCommand = new RelayCommand(ParamArrayAttribute => this.KickUser());
                }
                return _kickUserCommand;
            }
        }

        public ICommand BanUserCommand
        {
            get
            {
                if (_banUserCommand == null)
                {
                    _banUserCommand = new RelayCommand(ParamArrayAttribute => this.BanUser());
                }
                return _banUserCommand;
            }
        }

        #endregion

        #region Methods

        private void StartServer()
        {
            if (_serverListener == null)
            {
                _serverListener = new ServerListener();
            }
            _serverListener.Start();
            ServerStatus = "Server is running....";
        }

        private void StopServer()
        {
            if (_serverListener == null) return;
            _serverListener.Stop();
            ServerStatus = "Server is not running";
        }

        private void KickUser()
        {
            Command kickUserCommand = new KickUserCommand("Drewan");
            kickUserCommand.Execute();
        }

        private void BanUser()
        {
            // TODO: ban user
            // TODO: show admin that user was banned
        }

        protected void OnPropertyChanged(string propertyName)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
            }
        }

        #endregion

        #region Event Handling Methods

        public void OnUserConnect(object sender, EventArgs e)
        {
            ConnectionManager cm = sender as ConnectionManager;
            ActiveUsers = new List<string>(cm.GetConnectionDictionary().Keys);
        }

        public void OnUserDisconnect(object sender, EventArgs e)
        {
            ConnectionManager cm = sender as ConnectionManager;
            ActiveUsers = new List<string>(cm.GetConnectionDictionary().Keys);
        }

        #endregion
    }
}
